from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework.exceptions import APIException
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
from rest_framework.viewsets import GenericViewSet
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from utils.new_common_response import common_data
from .serializerls import LoginSerializers, AlterMessageSerializers, UserDetailSerializer, GetUserDetailModelSerializer
from utils.common_response import APIResponse
from user import models
# Create your views here.


# {"icon":"https://thirdwx.qlogo.cn/mmopen/vi_32/5UHydV26CHYIs1cmOWQHklFoWveNzxkumr8bwuSCdIGCdLg0Z0GpuicRZkEgZt0jRicoV0YiaHk53XCqvM9W5Bh1w/132","username"}
class UserInfor(GenericViewSet, CreateModelMixin):
    serializer_class = LoginSerializers


    @action(methods=["POST"],detail=False)
    def reg_login(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            token = serializer.context.get('token')
            id = serializer.context.get('id')
            return APIResponse(id=id,token=token)
        else:
            raise APIException(serializer.errors)
#实名认证
class RealNameAuthentication(GenericViewSet, CreateModelMixin):
    serializer_class = AlterMessageSerializers
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def get_serializer_class(self):  # 写在一个视图中，用不同的序列化类
        if self.action == 'user_detail':  # 路由后缀为sms_login   用SmsLoginSerializers序列类
            return UserDetailSerializer
        else:
            return super().get_serializer_class()
    @action(methods=["POST"], detail=False)
    def alter_message(self, request, *args, **kwargs):
        """
        实名认证
        """
        # id = request.data.get('id')  # 获取到用户id
        # mobile = request.data.get('mobile')  # 获取到用户手机号
        # print(id,mobile)
        serializer = self.get_serializer(data=request.data,context={"request":request})
        serializer.is_valid(raise_exception=True)
        return APIResponse(code =100,msg='修改成功')

    @action(methods=["POST"], detail=False)
    def user_detail(self, request, *args, **kwargs):     #{name:"ffjfjfjjfjjj",introduce:"dfjolioiiiiii"}
        """
        修改个人详情
        """
        serializer = self.get_serializer(data=request.data,context={"request":request})
        serializer.is_valid(raise_exception=True)
        photo_url = serializer.context.get('photo_url')
        desc = serializer.context.get('desc')
        name = serializer.context.get('name')



        return APIResponse(code=100,photo_url = photo_url,desc=desc,name=name)

class GetUserDetailAuthentication(GenericViewSet):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]
    # queryset = models.User.objects.filter(id=1).all()
    serializer_class = GetUserDetailModelSerializer

    # @action(methods=["POST"], detail=False)
    # def get_user_detaile(self, request, *args, **kwargs):
    #     return
    def retrieve(self, request, *args, **kwargs):
        # response = super().retrieve(request, *args, **kwargs)
        # old_data = response.data
        # new_data = common_data(old_data)
        # response.data = new_data
        serializer = self.get_serializer(data=request.data, context={"request": request})
        serializer.is_valid(raise_exception=True)
        photo_url = serializer.context.get('qiniu_link')
        desc = serializer.context.get('desc')
        name = serializer.context.get('name')

        return APIResponse(code=100,photo_url = photo_url,desc=desc,name=name)